package hoang.android.app.ui.missatool;

import hoang.android.app.R;
import hoang.android.app.model.Account;
import hoang.android.app.ui.BaseActivity;
import hoang.android.app.utility.StringUtility;

import java.util.regex.Pattern;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;

public class personalInfoActivity extends BaseActivity {

	public final Pattern EMAIL_ADDRESS_PATTERN = Pattern
			.compile("[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}");

	private Spinner spGender, spState;
	private EditText etFistName, etLastName, etMiddleInital, etZipcode, etCity,
			etAddress, etEmail, etPhone1, etPhone2, etPhone3, etPhone4;
	private CheckBox cbMail;
	ImageView imgNext;

	private String gender, state;
	private boolean isCheckMail = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_personal_info);

		loadUI();
		listenUI();
		setHeader(true,
				baseActivity.getResources().getString(R.string.personalinfo));
	}
	

	@Override
	protected void loadUI() {
		super.loadUI();

		spState = (Spinner) findViewById(R.id.sp_state);
		spGender = (Spinner) findViewById(R.id.sp_gender);

		etFistName = (EditText) findViewById(R.id.et_first_name);
		etLastName = (EditText) findViewById(R.id.et_last_name);
		etMiddleInital = (EditText) findViewById(R.id.et_middle_name);
		etZipcode = (EditText) findViewById(R.id.et_zipcode);
		etCity = (EditText) findViewById(R.id.et_city);
		etAddress = (EditText) findViewById(R.id.et_address);
		etEmail = (EditText) findViewById(R.id.et_email);
		etPhone1 = (EditText) findViewById(R.id.et_phone_1);
		etPhone2 = (EditText) findViewById(R.id.et_phone_2);
		etPhone3 = (EditText) findViewById(R.id.et_phone_3);
		etPhone4 = (EditText) findViewById(R.id.et_extends);

		cbMail = (CheckBox) findViewById(R.id.cb_email);

		imgNext = (ImageView) findViewById(R.id.img_next);

	}

	public void listenUI() {

		spGender.setAdapter(new ArrayAdapter<String>(baseActivity,
				android.R.layout.simple_spinner_item, getResources()
						.getStringArray(R.array.gender)));
		spState.setAdapter(new ArrayAdapter<String>(baseActivity,
				android.R.layout.simple_spinner_item, getResources()
						.getStringArray(R.array.state)));

		findViewById(R.id.img_next).setOnClickListener(onClickNext);

		spGender.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				gender = parent.getItemAtPosition(position).toString();
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				gender = "Mr";
			}
		});

		spState.setOnItemSelectedListener(selectState);
		cbMail.setOnCheckedChangeListener(checkChanged);
	}

	@Override
	public void onClick(View v) {
		super.onClick(v);

		switch (v.getId()) {
		case R.id.img_next:
			// startActivity(new Intent(baseActivity,
			// VehicleInfoActivity.class));
			break;

		default:
			break;
		}
	}

	OnClickListener onClickNext = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// tai day se kiem tra tat ca dau vao (validate)
			// neu du lieu ok -> se luu lai va chuyen sang giao dien khac
			onClickNextButton();
		}
	};

	OnCheckedChangeListener checkChanged = new OnCheckedChangeListener() {

		@Override
		public void onCheckedChanged(CompoundButton buttonView,
				boolean isChecked) {
			// TODO Auto-generated method stub
			isCheckMail = isChecked;
		}
	};

	OnItemSelectedListener selectState = new OnItemSelectedListener() {

		@Override
		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			// TODO Auto-generated method stub
			state = arg0.getItemAtPosition(arg2).toString();
		}

		@Override
		public void onNothingSelected(AdapterView<?> arg0) {
			// TODO Auto-generated method stub
			state = "AK";
		}
	};

	// --------------check email-------------------------------------//
	private boolean checkEmail(String paramString) {
		return this.EMAIL_ADDRESS_PATTERN.matcher(paramString).matches();
	}

	// -------------check validate-------------------------------------//
	public boolean checkValidate() {
		if ((StringUtility.isEmpty(this.etLastName))
				|| (StringUtility.isEmpty(this.etFistName))
				|| (StringUtility.isEmpty(this.etAddress))
				|| (StringUtility.isEmpty(this.etCity))
				|| (StringUtility.isEmpty(this.etZipcode))
				|| (StringUtility.isEmpty(this.etEmail))) {

			showPositiveCustomDialog(baseActivity, R.string.error,
					R.string.input_from_error, R.drawable.button_ok);
			this.etLastName.setBackgroundResource(R.drawable.bg_edittext_error);
			this.etFistName.setBackgroundResource(R.drawable.bg_edittext_error);
			this.etAddress.setBackgroundResource(R.drawable.bg_edittext_error);
			this.etCity.setBackgroundResource(R.drawable.bg_edittext_error);
			this.etZipcode.setBackgroundResource(R.drawable.bg_edittext_error);
			this.etEmail.setBackgroundResource(R.drawable.bg_edittext_error);

			return false;
		}

		// check phone
		if ((!StringUtility.isEmpty(this.etPhone1) && (StringUtility
				.isEmpty(this.etPhone2) || StringUtility.isEmpty(this.etPhone3)))
				|| (!StringUtility.isEmpty(this.etPhone2) && (StringUtility
						.isEmpty(this.etPhone1) || StringUtility
						.isEmpty(this.etPhone3)))
				|| (!StringUtility.isEmpty(this.etPhone3) && (StringUtility
						.isEmpty(this.etPhone1) || StringUtility
						.isEmpty(this.etPhone2)))
				|| (!StringUtility.isEmpty(this.etPhone3)
						&& !StringUtility.isEmpty(this.etPhone2)
						&& !StringUtility.isEmpty(this.etPhone3) && (etPhone1
						.getText().length() != 3
						|| etPhone2.getText().length() != 3 || etPhone3
						.getText().length() != 4))
				|| ((StringUtility.isEmpty(this.etPhone1)
						|| StringUtility.isEmpty(this.etPhone2) || StringUtility
							.isEmpty(this.etPhone3)) && !StringUtility
						.isEmpty(this.etPhone4))
				|| (!StringUtility.isEmpty(this.etPhone1)
						&& !StringUtility.isEmpty(this.etPhone2)
						&& !StringUtility.isEmpty(this.etPhone3)
						&& !StringUtility.isEmpty(this.etPhone4) && ((etPhone1
						.getText().length() != 3)
						|| (etPhone2.getText().length() != 3)
						|| (etPhone3.getText().length() != 4) || (etPhone4
						.getText().length() != 3)))) {

			etPhone1.setBackgroundResource(R.drawable.bg_edittext_error);
			etPhone2.setBackgroundResource(R.drawable.bg_edittext_error);
			etPhone3.setBackgroundResource(R.drawable.bg_edittext_error);
			etPhone4.setBackgroundResource(R.drawable.bg_edittext_error);

			showPositiveCustomDialog(baseActivity, R.string.error,
					R.string.phone_error, R.drawable.button_ok);
			return false;
		}

		if (this.etFistName.getText().toString().length()
				+ this.etLastName.getText().toString().length() >= 40) {
			showPositiveCustomDialog(baseActivity, R.string.error,
					R.string.first_last_name_error, R.drawable.button_ok);

			this.etFistName.requestFocus();
			this.etFistName.setBackgroundResource(R.drawable.bg_edittext_error);
			this.etLastName.setBackgroundResource(R.drawable.bg_edittext_error);
			return false;
		}
		if (this.etZipcode.getText().toString().length() != 5) {
			showPositiveCustomDialog(baseActivity, R.string.error,
					R.string.zipcode_error, R.drawable.button_ok);
			this.etZipcode.requestFocus();
			this.etZipcode.setBackgroundResource(R.drawable.bg_edittext_error);
			return false;
		}
		if (!checkEmail(this.etEmail.getText().toString())) {
			showPositiveCustomDialog(baseActivity, R.string.error,
					R.string.email_error, R.drawable.button_ok);
			this.etEmail.requestFocus();
			this.etEmail.setBackgroundResource(R.drawable.bg_edittext_error);
			return false;
		}

		return true;
	}

	// -------------click next----------------------//
	 public void onClickNextButton() {
		  if (checkValidate()) {

		   // dung doi tuong Account de luu tru tat ca thong in ma nguoi dung
		   // nhap vao
		   Account account = new Account();
		   account.setAddress_title(gender);
		   account.setAddress_first_name(etFistName.getText().toString().trim());
		   account.setAddress_last_name(etLastName.getText().toString().trim());
		   account.setAddress_middle_name(etMiddleInital.getText().toString().trim());
		   account.setAddress1(etAddress.getText().toString().trim());
		   account.setAddress_city(etCity.getText().toString().trim());
		   account.setAddress_zipcode(etZipcode.getText().toString().trim());
		   account.setAddress_state(state);
		   account.setWorkphone(etPhone1.getText().toString().trim()
		     + etPhone2.getText().toString().trim()
		     + etPhone3.getText().toString().trim());
		   account.setAddressEmail(etEmail.getText().toString().trim());
		   account.setEmail_receipt(isCheckMail);

		   application.setAccount(account);

		   startActivity(new Intent(baseActivity, VehicleInfoActivity.class));

		  }
		 }
}
